local app = require "app"
local game = require "club.threecard.hbsl"
local LoopScrollList = require "blaze.ui.widget.LoopScrollList"

local Item = game.ui.element()

function Item:ctor()
    local avatar_icon = require "ui.Common.AvatarIcon"
    self.avatar = avatar_icon.new()
end

function Item:on_scroll_cell_update(data, index)
    self.no_1:SetActive(index == 1)
    self.no_2:SetActive(index == 2)
    self.no_3:SetActive(index == 3)

    self.no_other_txt.gameObject:SetActive(index ~= 1 and index ~= 2 and index ~= 3)
    self.no_other_txt.text = index

    self.avatar:set_head(data.head)
    self.avatar:set_flag(data.country)
    self.name_txt.text = data.name
    self.chip_txt.text = app.i18n.number(data.chip, "chip3")
end

function Item:on_injected()
    self.avatar:set_gameobject(self.avatar_obj, true)
end

local M = game.ui.element()

function M:ctor()
    self.res = "club.threecard.hbsl/ui/ingame.b:others_record"

    self.list = LoopScrollList.new()
    self.list.res = "club.threecard.hbsl/ui/ingame.b:other_item"
    self.list.create_item = function(_, data, index) return Item.new() end
end

function M:on_load()
    self:super("on_load")
    self.list:load()
end

function M:on_injected(bind)
    self.list:attach(self.scroll)
    bind("button.click", self.btn_close, self.close_ivk)
    bind("button.click", self.btn_mask, self.close_ivk)
end

function M:close_ivk()
    self:hide()
    game.audio.post("hbsl:click")
end

function M:show()
    self:super("show")

    local data = {}

    for _, player in pairs(game.store.gameplay.state.players) do
        table.insert(data, player)
    end

    table.sort(data, function(p1, p2)
        return p1.chip > p2.chip
    end)

    self.list:fill(data)
end

return M